package com.isyscore.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.isyscore.dto.sys.AddDeptForm;
import com.isyscore.dto.sys.DeptTreeDto;
import com.isyscore.dto.sys.ListDeptForm;
import com.isyscore.entity.admin.SysDept;
import com.isyscore.service.admin.SysDeptService;
import com.isyscore.utils.MesResult;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author LGL
 * @since 2024年10月25日
 */
@RestController
@RequestMapping("/api/sysDept")
@Tag(name = "部门管理", description = "部门管理相关接口")
public class SysDeptController {
    @Autowired
    private SysDeptService sysDeptService;

    @PostMapping("/list")
    @RequiresPermissions("system:dept:view")
    @Operation(summary = "获取部门列表")
    public MesResult list(@RequestBody ListDeptForm form) {
        List<DeptTreeDto> list = sysDeptService.getDeptList(form);
        return MesResult.success(list);
    }

    @PostMapping("/add")
    @RequiresPermissions("system:dept:add")
    @Operation(summary = "新增部门")
    public MesResult insert(@RequestBody AddDeptForm form) {
        //获取登录id
        Object userId = SecurityUtils.getSubject().getPrincipal();
        form.setCreateBy(userId.toString());
        return MesResult.success(sysDeptService.add(form));
    }
    @PostMapping("/update")
    @RequiresPermissions("system:dept:edit")
    @Operation(summary = "修改部门")
    public MesResult update(@RequestBody SysDept dept) {
        Object userId = SecurityUtils.getSubject().getPrincipal();
        dept.setUpdateBy(userId.toString());
        return MesResult.success(sysDeptService.edit(dept));
    }
    @DeleteMapping("/delete/{deptId}")
    @RequiresPermissions("system:dept:remove")
    @Operation(summary = "删除部门")
    public MesResult delete(@PathVariable("deptId") Long deptId) {
        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
        return MesResult.success(sysDeptService.delete(deptId,userId));
    }





}

